/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Robot;


import lejos.nxt.SensorPort;

/**
 *
 * @author zach
 */
public class ACLightSensor extends lejos.nxt.LightSensor{

    private int baseline=0;
    private int num_runs=100;

    public ACLightSensor(SensorPort port, boolean floodlight) {
        super(port, floodlight);
        setBaseLine();
    }
    public ACLightSensor(SensorPort port) {
        super(port);
        setBaseLine();
    }

    public int getBaseline(){
        return baseline;
    }

    public int getCalabratedValue(){
        return super.readNormalizedValue()-baseline;
    }

    private void setBaseLine(){
        int[] zig=new int[num_runs];
        for(int i=0;i<num_runs;i++){
            zig[i]=super.readNormalizedValue();
            try {
                Thread.sleep(20);
            } catch (InterruptedException ex) {
                //Logger.getLogger(ACLightSensor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        baseline=adv(zig);
    }
    private int adv(int[] in){
        int z=0;
        for(int i:in)
            z+=i;
        return z/in.length;
    }
}
